package com.arvato.minipro.controller.front.mall;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.arvato.minipro.bean.bean_do.mall.EcCutDo;
import com.arvato.minipro.bean.bean_do.mall.EcCutItemDo;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.minipro.bean.bean_vo.mall.EcCutVo;
import com.arvato.minipro.service.mall.CutDownService;

/**
 * 
 * @author: smallking
 * 
 * @Descibe: 处理用户砍价的Service
 * 
 * @Date: 2018年7月6日  下午1:37:21
 */
@Controller
@RequestMapping(value = "/mall/cut")
public class CutDownController {
	
	@Autowired
	private CutDownService cutDownService;
	
	/**
	 * 查询一个砍价的详情
	 * 可以根据cutID查，也可以根据 commodityCutId + userId来查
	 * 当根据commodityCutId userId来查的时候，要先查询当前砍价商品是否被当前人砍价了
	 * 如果不在，就执行插入，并且返回详情；
	 * 如果在就直接返回详情；
	 * @param addressDo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/detail", method = {RequestMethod.POST})
	public Result insert(HttpServletRequest req, String cutId, String commodityCutId, String userId){
		Result result = new Result(false);
		EcCutVo cutVo = null;
		Query query = new Query();
		query.put("userId", userId);
		if(cutId != null) {
			cutVo = cutDownService.getCutVoById(cutId);
		}else {
			// 先查询当前商品是否已经在当前人的砍价列表
			query.put("commodityCutId", commodityCutId);
			cutVo = cutDownService.getCutByQuery(query);
			if(cutVo == null) {
				// 如果不在的话 就插入
				EcCutDo cutDo = new EcCutDo();
				cutDo.setCommodityCutId(commodityCutId);
				cutDo.setStatus(1);
				cutDo.setUserId(userId);
				cutDo.setCreateTime(new Date());
				try {
					int row = cutDownService.insert(cutDo);
					if(row == 1) {
						// 如果插入成功,就执行查询，并且返回详情
						cutVo = cutDownService.getCutVoById(cutDo.getCutId());
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		if(cutVo != null) {
			result.setData(cutVo);
			result.setResult(true);
		}
		return result;
	}
	
	/**
	 * 用户砍价
	 * @param req
	 * @param userId
	 * @param cutId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/down", method = {RequestMethod.POST})
	public Result cutDown(HttpServletRequest req, EcCutItemDo cutItemDo){
		Result result = new Result(false);
		cutItemDo.setCreateTime(new Date());
		try {
			int row = cutDownService.insert(cutItemDo);
			if(row == 1) {
				// 如果新增成功， 检查当前的砍价人数是否已达上限
				EcCutVo cutVo = cutDownService.getCutVoById(cutItemDo.getCutId());
				if(cutVo.getCommodityCutVo().getCutCount() == cutVo.getCutItemList().size()) {
					// 如果达到上线了，更新当前 cut状态 
					EcCutDo cutDo = new EcCutDo();
					cutDo.setCutId(cutVo.getCutId());
					cutDo.setStatus(2);
					row = cutDownService.update(cutDo);
				}
				result.setResult(true);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	
}
